﻿using System;

using TrueCode.DependencyManager;
using TrueCode.DependencyManager.BuildActions.BuidBit;
using TrueCode.DependencyManager.BuildActions.BuildBit;
using TrueCode.DependencyManager.Extensions;
using TrueCode.DependencyManager.Extensions.Action.Typed;

namespace EMan.Common.DependencyInjection
{
  public class Factory : IFactory
  {
    private readonly Container _container;

    public Factory( Container container )
    {
      _container = container;
      _container.RegisterInstance< IFactory >( this );
    }

    public object Resolve( Type type, params object[] paramеters )
    {
      var actions = new BuildActionCollection();
      actions.AddDefault< IBuildBit >( new BuildBit() );

      if( paramеters.Length > 0 )
        actions.AddDefault< IGetSessionalParameter >( new SessionalParameterBuildAction( paramеters ) );

      return _container.Resolve( type, null, actions, null );
    }

    public T Resolve< T >(params object[] paramеters)
    {
      var actions = new BuildActionCollection();
      actions.AddDefault< IBuildBit >( new BuildBit() );

      if( paramеters.Length > 0 )
        actions.AddDefault< IGetSessionalParameter >( new SessionalParameterBuildAction( paramеters ) );

      return _container.Resolve< T >( actions );
    }
  }
}